﻿using BaseData.Domain;
using BaseData.Infrastructure.Interfaces;
using Essensoft.Paylink.Alipay;
using Essensoft.Paylink.Alipay.Domain;
using Essensoft.Paylink.Alipay.Notify;
using Essensoft.Paylink.Alipay.Request;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using NPOI.XWPF.UserModel;
using System.Text;

namespace BaseData.API.Read.Controllers
{
    /// <summary>
    /// 支付宝支付控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AlipayController : ControllerBase
    {
        private readonly ILogger<AlipayController> _logger;
        private readonly IAlipayClient _client;
        private readonly IOptions<AlipayOptions> _optionsAccessor;
        private readonly IMediator mediator;
        private readonly IBaseRepository<Domain.Amount> amountRep;


        /// <summary>
        /// 构造函数
        /// </summary>
        public AlipayController(ILogger<AlipayController> logger, IAlipayClient client, IOptions<AlipayOptions> optionsAccessor, IMediator mediator,IBaseRepository<Domain.Amount> amountRep)
        {
            _logger = logger;
            _client = client;
            _optionsAccessor = optionsAccessor;
            this.mediator = mediator;
            this.amountRep = amountRep;
        }



        /// <summary>
        /// 电脑网站支付
        /// </summary>
        /// <param name="orderId">订单号</param>
        /// <param name="orderMoney">订单金额</param>
        [HttpPost]
        public async Task<IActionResult> PagePay(string AmountId, decimal PaymentAmount)
        {
            var Amount = new Domain.Amount()
            {
                OperatingNum = 200+AmountId,
                Source = "微信小程序",
                PaymentMethod = "支付宝",
                PaymentAmount = PaymentAmount,
                PaymentStatus = false, // 待支付
                Balance = 100,
                OperatingTime = DateTime.Now,
                BusinessId = 1,
            };
            await amountRep.AddAsync(Amount);

            
            //字段已更改
            var model = new AlipayTradePagePayModel
            {
                //订单详细描述信息
                Body = $"订单编号为：{AmountId}，支付金额为：{PaymentAmount}元",

                //标题
                Subject = $"订单编号为：{AmountId}的订单支付",
                //订单金额
                TotalAmount = PaymentAmount.ToString(),
                //商户订单号
                OutTradeNo = AmountId,
                //产品编码
                ProductCode = "FAST_INSTANT_TRADE_PAY"
            };
            

            var req = new AlipayTradePagePayRequest();
            //完成支付操作
            req.SetBizModel(model);
                //设置支付成功后的回调地址
                req.SetNotifyUrl("http://a68b6a78.natappfree.cc/api/AlipavNotify/PagePay");
            //设置支付成功后的跳转地址（前台地址）
            req.SetReturnUrl("http://1ocalhost:5174");
            var response = await _client.PageExecuteAsync(req, _optionsAccessor.Value); 
            return Content(response.Body, "text/html", Encoding.UTF8);
        }


      
        }


    }

        


    
